package com.itheima.thread.threadPool.executorService;


import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @Author: Ethan Yankang
 * @Program: Interview
 * @Date: 2024-08-15 19:08
 **/
public class newFixedThreadPool_my {
    public static void main(String[] args) {
//        test1();
        test2();
    }

    private static void test1() {
//        使用手动创建对象的方式来创建线程
        ExecutorService executorService = Executors.newFixedThreadPool(3);
//        提交任务
        System.out.println(newFixedThreadPool_my.class+"test1()");
        for (int i = 1; i < 11; i++) {
            executorService.submit(new MyRunnable(i));
        }
//        关闭线程池，仅仅是不再接受新的任务，以前的任务还会继续执行
        executorService.shutdown();
    }

    private static void test2() {
//       使用线程池来创建线程对象
        System.out.println(newFixedThreadPool_my.class+"test()2:");
        ExecutorService executorService = Executors.newFixedThreadPool(3, new ThreadFactory() {

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"这是采用固定线程数量的线程池的方法重写的线程");
            }
        });
        for (int i = 1; i < 11; i++) {
            executorService.submit(new MyRunnable(i));
         }
//        关闭线程池，如果还有没有执行完的线程，则取消执行。并返回这些任务。
        List<Runnable> runnableList = executorService.shutdownNow();
        System.out.println("------------------------------------------");
        for (Runnable runnable : runnableList) {
            System.out.println(runnable);
        }
    }
}
